﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace JobSeek
{
    public partial class applicant_login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            using (job_seekerEntities ctx = new job_seekerEntities())
            {
                string email = tbxUsername.Text;
                string password = tbxPassword.Text;

                string hashPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                var result = from user in ctx.Applicants
                             where user.email == email
                             where user.password == hashPassword
                             select user;

                if (result.Count() == 1)
                {
                    //user will be logged in.

                    FormsAuthentication.SetAuthCookie(email, false);

                    //check for user's role and redirect them accordingly


                    if (Roles.IsUserInRole(email, "applicant"))
                    {
                        Response.Redirect("#to employer page");
                    }
                }
                else
                {
                    //return wrong username or password message.
                    Response.Write("wrong username or password");
                }

            }
        }
    }
}